package com.liumuxian.video.service.impl;

import com.liumuxian.video.dao.*;
import com.liumuxian.video.pojo.Templateset;
import com.liumuxian.video.service.TemplatesetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;
import java.util.*;

@Service
@Transactional
public class TemplatesetServiceImpl implements TemplatesetService {
    @Autowired
    private TemplatesetDao templatesetDao;
    @Autowired
    private IdWorker idWorker;
    @Override
    public void save(Templateset templateset) {
        templateset.setId(idWorker.nextId()+"");
        templatesetDao.save(templateset);
    }

    @Override
    public void update(Templateset templateset) {
        templatesetDao.update(templateset.getPid(),templateset.getName(), templateset.getType(),
        templateset.getState(), templateset.getIstop(),templateset.getVisits(),templateset.getWeight(),
        templateset.getCreatdata(),templateset.getUpdatedate(), templateset.getId());
    }

    @Override
    public List<Templateset> findAllByState(Integer state) {
        return templatesetDao.findAllByState(state);
    }

    @Override
    public Templateset getOne(String tid) {
        return templatesetDao.getOne(tid);
    }

    @Override
    public List<Templateset> findByTemAndVideo(String tid) {
        return templatesetDao.findByTemAndVideo(tid);
    }

    @Override
    public Page<Templateset> pageTemplateset(Integer page , Integer size){
        PageRequest pageRequest =  PageRequest.of(page-1, size);
        Page<Templateset> examInfos=templatesetDao.findAll(pageRequest);
        List<Templateset> templatesets = new ArrayList<>();
        examInfos.getContent().forEach(ei -> {
            Templateset examInfoVO = new Templateset();
            BeanUtils.copyProperties(ei,examInfoVO);
            templatesets.add(examInfoVO);
        });
        return new PageImpl<Templateset>(templatesets, pageRequest, examInfos.getTotalElements());
    }
}
